package pos.utils;

import java.awt.event.MouseEvent;
import javax.media.j3d.*;
import javax.vecmath.*;

/**
 *一个用java3d模拟的图片按钮
 * @author plus
 */
public class ImageButton2D extends TransformGroup implements Pickable {

    private Plane2D plane;
    private Transform3D myTransform;
    private Color3f colorIdle,  colorHot,  colorClicked;
    private String eventName;
    private IContainer myHost;

    /**
     * 构造一个图片按钮
     * @param pos 图片按钮中央点的位置
     * @param width 图片按钮的宽度
     * @param height 图片按钮的高度
     * @param imgKey 图片按钮图片路径在路径资源库中的键
     * @param colorIdle 图片按钮待命时的颜色叠加
     * @param colorHot 图片按钮选中时的颜色叠加
     * @param colorClicked 图片按钮触发时的颜色叠加
     * @param eventName 图片按钮触发时引起的事件名
     */
    public ImageButton2D(Vector3d pos, float width, float height, String imgKey,
            Color3f colorIdle, Color3f colorHot, Color3f colorClicked, String eventName) {
        plane = (Plane2D) ObjectFactory.createTexturedSurface(width, height,(float)pos.z, colorIdle, 0f, PathRes.getPath(imgKey));
        myTransform = new Transform3D();
        myTransform.set(new Vector3d(pos.x,pos.y,0));
        this.setTransform(myTransform);
        this.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        this.colorIdle = colorIdle;
        this.colorHot = colorHot;
        this.colorClicked = colorClicked;
        this.addChild(plane);
        this.eventName = eventName;
    }
    
    /**
     * 构造一个指定是否总是可见的图片按钮
     * @param pos 图片按钮中央点的位置
     * @param width 图片按钮的宽度
     * @param height 图片按钮的高度
     * @param imgKey 图片按钮图片路径在路径资源库中的键
     * @param colorIdle 图片按钮待命时的颜色叠加
     * @param colorHot 图片按钮选中时的颜色叠加
     * @param colorClicked 图片按钮触发时的颜色叠加
     * @param eventName 图片按钮触发时引起的事件名
     * @param bAlwaysShown 图片按钮是否总是可见
     */
    public ImageButton2D(Vector3d pos, float width, float height, Texture2D texture,
            Color3f colorIdle, Color3f colorHot, Color3f colorClicked, String eventName,boolean bAlwaysShown) {
        plane = (Plane2D) ObjectFactory.createTexturedSurface(
                width, height, (float)pos.z,colorIdle, 0f, texture,bAlwaysShown);
        myTransform = new Transform3D();
        myTransform.set(new Vector3d(pos.x,pos.y,0));
        this.setTransform(myTransform);
        this.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        this.colorIdle = colorIdle;
        this.colorHot = colorHot;
        this.colorClicked = colorClicked;
        this.addChild(plane);
        this.eventName = eventName;
    }
    
        /**
     * 构造一个图片按钮
     * @param pos 图片按钮中央点的位置
     * @param width 图片按钮的宽度
     * @param height 图片按钮的高度
     * @param imgKey 图片按钮图片路径在路径资源库中的键
     * @param colorIdle 图片按钮待命时的颜色叠加
     * @param colorHot 图片按钮选中时的颜色叠加
     * @param colorClicked 图片按钮触发时的颜色叠加
     * @param eventName 图片按钮触发时引起的事件名
     */
    public ImageButton2D(Vector3d pos, float width, float height, Texture2D texture,
            Color3f colorIdle, Color3f colorHot, Color3f colorClicked, String eventName) {
        plane = (Plane2D) ObjectFactory.createTexturedSurface(width, height, 
                (float)pos.z,colorIdle, 0f, texture);
        myTransform = new Transform3D();
        myTransform.set(new Vector3d(pos.x,pos.y,0));
        this.setTransform(myTransform);
        this.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        this.colorIdle = colorIdle;
        this.colorHot = colorHot;
        this.colorClicked = colorClicked;
        this.addChild(plane);
        this.eventName = eventName;
    }
    
    /**
     * 构造一个指定是否总是可见的图片按钮
     * @param pos 图片按钮中央点的位置
     * @param width 图片按钮的宽度
     * @param height 图片按钮的高度
     * @param imgKey 图片按钮图片路径在路径资源库中的键
     * @param colorIdle 图片按钮待命时的颜色叠加
     * @param colorHot 图片按钮选中时的颜色叠加
     * @param colorClicked 图片按钮触发时的颜色叠加
     * @param eventName 图片按钮触发时引起的事件名
     * @param bAlwaysShown 图片按钮是否总是可见
     */
    public ImageButton2D(Vector3d pos, float width, float height, String imgKey,
            Color3f colorIdle, Color3f colorHot, Color3f colorClicked, String eventName,boolean bAlwaysShown) {
        plane = (Plane2D) ObjectFactory.createTexturedSurface(
                width, height,(float)pos.z,
                colorIdle, 0f, PathRes.getPath(imgKey),bAlwaysShown);
        myTransform = new Transform3D();
        myTransform.set(new Vector3d(pos.x,pos.y,0));
        this.setTransform(myTransform);
        this.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        this.colorIdle = colorIdle;
        this.colorHot = colorHot;
        this.colorClicked = colorClicked;
        this.addChild(plane);
        this.eventName = eventName;
    }
    /**
     * 设置图片按钮的位置
     * @param pos 图片按钮中央点的新坐标
     */
    public void setPosition(Vector3d pos) {
        myTransform.set(pos);
        this.setTransform(myTransform);
    }
    
    /**
     * 设置图片按钮的缩放比例
     * @param scale 图片按钮新的缩放比例，原始值为1.0
     */
    public void setScale(double scale){
        Transform3D transScale=new Transform3D(),transMoveInverted=new Transform3D(myTransform);
        transScale.setScale(new Vector3d(scale,scale,scale));
        transMoveInverted.mul(transScale);
        this.setTransform(transMoveInverted);
    }
    
    public void setAlpha(float alpha){
        plane.setAlpha(alpha);
    }
    /**
     * 为鼠标拾取行为调用的方法
     * @param evt 拾取当此对象的鼠标事件
     */
    public void picked(MouseEvent evt) {
        switch (evt.getID()) {
            case MouseEvent.MOUSE_PRESSED: {
                plane.setColor(colorClicked);
                break;
            }
            case MouseEvent.MOUSE_RELEASED: {
                plane.setColor(colorHot);
                break;
            }
            case MouseEvent.MOUSE_DRAGGED: {
                plane.setColor(colorIdle);
                break;
            }
            case MouseEvent.MOUSE_MOVED: {
                plane.setColor(colorHot);
                myHost.doEvent("SetHotItem", this);
                break;
            }
            case MouseEvent.MOUSE_CLICKED: {
                //plane.setColor(colorClicked);
                myHost.doEvent(eventName, this);
                break;
            }
        }
    }

    /**
     * 设置图片按钮当前在容器中的状态(待命||选中||触发)
     * @param statue Dock当前在容器中的状态
     */
    public void setStatue(int statue) {
        switch (statue) {
            case Idle: {
                plane.setColor(colorIdle);
                break;
            }
            case Hot: {
                plane.setColor(colorHot);
                break;
            }
            case Action: {
                plane.setColor(colorClicked);
                myHost.doEvent(eventName, null);
                break;
            }
        }
    }
    
    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public void setEnable(boolean bEnable){
        plane.setPickable(bEnable);
    }
    /**
     * 设置此图片按钮的拥有容器
     * @param host 拥有容器
     */
    public void setHost(IContainer host) {
        myHost=host;
    }
    
}
